#include <gtk/gtk.h>   
#include <gst/gst.h>   
#include <signal.h>   
#include <string.h>   
#include <gst/interfaces/xoverlay.h>
#include <gdk/gdkx.h>
#include <dirent.h>
#include <unistd.h>

#include "config.h"
#include "sys_log.h"

static char  file[100][1024];
static int file_index=0;
static int file_count=0;

static GtkWidget *player_gif1;
static GtkWidget *player_gif2;
static GtkWidget *player_gif3;


GtkWidget *create_gif_from_file(gchar* filepath)
{
	GtkWidget *image=NULL;
	GdkPixbufAnimation *pixbuf;
	 
	GError *error = NULL;

	pixbuf = gdk_pixbuf_animation_new_from_file(filepath,NULL);
	if(!pixbuf){
		  SYS_LOG("create_gif_from_file :path[%s] error[%s]",filepath,error->message);
		  g_error_free(error);	
	}
	image = gtk_image_new_from_animation (pixbuf);
    g_object_unref(pixbuf); 
	return image;
}


void gif_player_replace_gif(gchar* filepath,GtkWidget *image)
{
	gtk_image_set_from_file(GTK_IMAGE(image),filepath);
    gtk_widget_show(image);	  
}

static gboolean gif_player_replace_timeout(void *data)
{
	int f_index = (int)data;

	if(file_index ==  file_count) file_index = 0;
	gif_player_replace_gif(file[file_index],player_gif1);file_index++;
	
	if(file_index ==  file_count) file_index = 0;
	gif_player_replace_gif(file[file_index],player_gif2);file_index++;

	if(file_index ==  file_count) file_index = 0;
	gif_player_replace_gif(file[file_index],player_gif3);file_index++;

	return TRUE;
}

void play_gif_player(GtkWidget *table)
{
	if(file_count == 0) return;


	player_gif1 = create_gif_from_file(file[file_index]);file_index++;
//	gtk_box_pack_start_defaults(GTK_BOX(table), player_gif1);
	gtk_box_pack_start(GTK_BOX(table),player_gif1,FALSE,FALSE,0);
    gtk_widget_show(player_gif1);

	if(file_index ==  file_count) file_index = 0;
	player_gif2 = create_gif_from_file(file[file_index]);file_index++;
	//gtk_box_pack_start_defaults(GTK_BOX(table), player_gif2);
	gtk_box_pack_start(GTK_BOX(table),player_gif2,FALSE,FALSE,0);
    gtk_widget_show(player_gif2);

	if(file_index ==  file_count) file_index = 0;
	player_gif3 = create_gif_from_file(file[file_index]);file_index++;
	//gtk_box_pack_start_defaults(GTK_BOX(table), player_gif3);
	gtk_box_pack_start(GTK_BOX(table),player_gif3,FALSE,FALSE,0);
    gtk_widget_show(player_gif3);
	
	g_timeout_add(10000,(GSourceFunc)gif_player_replace_timeout,(gpointer)file_index);
}


int gif_player_read_filelist()
{
    DIR *dir;
    struct dirent *ptr;
    char base[1000];
	int i = 0;

	memset(base,'\0',sizeof(base));
	getcwd(base, 999);


	strcat(base,GIF_PATH);
	
    if ((dir=opendir(base)) == NULL)
    {
        SYS_LOG("Open dir error...");
        exit(1);
    }
	file_count = 0;
    while ((ptr=readdir(dir)) != NULL)
    {
        if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0)    ///current dir OR parrent dir
            continue;
        else if(ptr->d_type == 8) {    ///file
			if(g_strrstr(ptr->d_name,".gif")) {//store
				memset(file[i],0,1024);
				sprintf(file[i],"%s/%s",base,ptr->d_name);
				 SYS_LOG("file[%d]:%s\n",i,file[i]);
				i++;
				file_count++;
			}
		}
        else if(ptr->d_type == 10)    ///link file
            SYS_LOG("d_name:%s/%s\n",base,ptr->d_name);
     }
     closedir(dir);
     return 1;
}

